package com.gxa.service.Impl;

import com.gxa.common.R;
import com.gxa.common.State;
import com.gxa.mapper.TalentSaveMapper;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Params_Son;
import com.gxa.pojo.Resume;
import com.gxa.pojo.talentsave.*;
import com.gxa.service.TalentSaveService;
import com.gxa.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TalentSaveServiceImpl implements TalentSaveService{
    @Autowired(required = false)
    private TalentSaveMapper talentSaveMapper;
    /**
     * 显示已经录用简历的具体信息
     * @param returnResume_id
     * @return
     */
    @Override
    public R showAlreadyEmployResume(ReturnResume_id returnResume_id) {
        R r = new R();
        Integer resume_id = returnResume_id.getResume_id();
        Resume resume = talentSaveMapper.showAlreadyEmployResume(resume_id);
        if(resume==null){
            r.setCode(500);
            r.setMsg("显示已经录用简历的具体信息失败!空值！空值！");
        }else{
            r.setCode(200);
            r.setMsg("显示已经录用简历的具体信息成功!");
            r.setData(resume);
            return r;
        }
        return r;
    }
    /**
     * 显示已经工作简历的具体信息
     * @param returnResume_id
     * @return
     */
    @Override
    public R showAlreadyWorkResume(ReturnResume_id returnResume_id) {
        R r = new R();
        Integer resume_id = returnResume_id.getResume_id();
        Resume resume = talentSaveMapper.showAlreadyWorkResume(resume_id);
        if(resume==null){
            r.setCode(500);
            r.setMsg("显示已经工作简历的具体信息失败!空值！空值！");
        }else {
            r.setCode(200);
            r.setMsg("显示已经工作简历的具体信息成功!");
            r.setData(resume);
            return r;
        }

        return r;
    }
    /**
     * 查询部门下拉框
     * @param
     * @return
     */
    @Override
    public R selectAllDept() {
        R r = new R();
        List<Dept> list = talentSaveMapper.selectAllDept();
        if(list==null){
            r.setCode(500);
            r.setMsg("查询部门下拉框失败！空值！空值！");
        }else{
            r.setCode(200);
            r.setMsg("查询部门下拉框成功！");
            r.setData(list);
            return r;
        }
        return r;
    }

    /**
     * 查询职称下拉框
     * @return
     */
    @Override
    public R selectAllCompetent() {
        Integer param_id = State.PARAMS_TITLE;
        R r = new R();
        List<Params_Son> list = talentSaveMapper.selectAllCompetent(param_id);
        if(list==null){
            r.setCode(500);
            r.setMsg("查询职称下拉框失败！！空值警告");
        }else{
            r.setCode(200);
            r.setMsg("查询职称下拉框成功！");
            r.setData(list);
        }
        return r;
    }

    /**
     * 查询岗位下拉框
     * @return
     */
    @Override
    public R selectAllPost() {
        Integer param_id = State.PARAMS_POST;
        R r = new R();
        List<Params_Son> list = talentSaveMapper.selectAllPost(param_id);
        if(list==null){
            r.setCode(500);
            r.setMsg("查询岗位下拉框失败！！空值警告");
        }else{
            r.setCode(200);
            r.setMsg("查询岗位下拉框成功！");
            r.setData(list);
        }
        return r;
    }
    /**
     * 人才简历信息反馈
     * @param
     * @return
     */
    @Transactional
    @Override
    public R feedbackTalent(UpdateResume updateResume) {
        R r = new R();
        Integer result ;
        if(updateResume==null){
            r.setCode(500);
            r.setMsg("空值警告！反馈失败！");
        }else{

            if(updateResume.getRe_status()==0){
                updateResume.setRe_status(State.RESUME_EMPLOYED);
                System.out.println(updateResume.getRe_status());
                result=talentSaveMapper.feedbackTalent(updateResume);
                if(result>0){
                    r.setCode(200);
                    r.setMsg("反馈成功！");
                    r.setData(result);
                }else{
                    r.setCode(500);
                    r.setMsg("反馈失败!!");
                    r.setData(result);
                }
            }else{
                updateResume.setRe_status(State.RESUME_NO_EMPLOYED);
                result=talentSaveMapper.feedbackTalent(updateResume);
                System.out.println(updateResume.getRe_status());
                if(result>0){
                    r.setCode(200);
                    r.setMsg("反馈成功！！");
                    r.setData(result);
                }else{
                    r.setCode(500);
                    r.setMsg("反馈失败!");
                    r.setData(result);
                }
            }
            return r;
        }
        return r;
    }

    /**
     *员工入职
     * @param insertEmpUpdateResume
     * @return
     */
    @Transactional
    @Override
    public R joinTheStaff(InsertEmpUpdateResume insertEmpUpdateResume) {
        R r = new R();
        if(insertEmpUpdateResume==null){
            r.setCode(500);
            r.setMsg("员工入职失败！空值警告！");
        }else {
            Integer result = talentSaveMapper.joinTheStaff(insertEmpUpdateResume);
            Integer result1 = talentSaveMapper.insertTheEmpNumberAndRe_applyJob(insertEmpUpdateResume);
            talentSaveMapper.updateTheResume(insertEmpUpdateResume);
            if(result>0){
                r.setCode(200);
                r.setMsg("员工入职成功！");
                r.setData(result);
            }else{
                r.setData(500);
                r.setMsg("员工入职失败！");
                r.setData(result);
            }
            return r;
        }
        return r;
    }

    /**
     * 查找单个人才信息
     * @param myPages
     * @return
     */
    @Override
    public R queryTalentInfo(MyPages myPages) {
        R r = new R();
        if(myPages.getResumeinfo()==null||myPages.getCurrentPage()==null||myPages.getPageSize()==null||myPages==null){
            r.setCode(500);
            r.setMsg("空值！空值！查询失败！");
        }else {
            if(myPages.getResumeinfo().equals("已录用")){
                myPages.setResumeinfo("3");
            }else if(myPages.getResumeinfo().equals("未录用")){
                myPages.setResumeinfo("2");
            }else if(myPages.getResumeinfo().equals("已入职")){
                myPages.setResumeinfo("4");
            }else if(myPages.getResumeinfo().equals("未查看")){
                myPages.setResumeinfo("1");
            }
            String resumeinfo= myPages.getResumeinfo();
            Integer currentPage = myPages.getCurrentPage();
            Integer pageSize = myPages.getPageSize();
            Integer param1 =(currentPage -1)*pageSize;
            Integer param2 = pageSize;
            List<Object> list = talentSaveMapper.queryTalentInfo(resumeinfo,param1,param2);
            //Integer totalSize = talentSaveMapper.queryCount(resumeinfo);
            //list.add(totalSize);
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(list);
            return r;
        }
        return r;
    }

    /**
     * 显示所有人才信息接口
     * @param myPages
     * @return
     */
    @Override
    public R showTalentInfo(MyPages myPages) {
        R r = new R();
        Integer currentPage = myPages.getCurrentPage();
        Integer pageSize = myPages.getPageSize();
        Integer param1 =(currentPage -1)*pageSize;
        Integer param2 = pageSize;
        Integer param_id = State.PARAMS_DELETE;
        List<Object> list = talentSaveMapper.showTalentInfo(param1,param2);
//        Integer totalSize = talentSaveMapper.selectCount();
        //List<Params_Son> education  = talentSaveMapper.selectEducation(param_id);
        //list.add(education);
        //list.add(totalSize);
        if(list==null){
            r.setCode(200);
            r.setMsg("查询失败！空值警告！");
        }else{
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(list);
            return r;
        }
        return r;
    }

    /**
     * 查看单个人才具体信息
     * @param
     * @return
     */
    @Override
    public R showDetailedInfo(ReturnResume_id returnResume_id) {
        R r = new R();
        Integer resume_id = returnResume_id.getResume_id();
        Resume resume = talentSaveMapper.showDetailedInfo(resume_id);
        r.setCode(200);
        r.setMsg("查看信息成功！");
        r.setData(resume);
        return r;
    }

    /**
     * 显示未查看简历
     * @return
     */
    @Override
    public R notCheckResume() {
        Integer re_status = State.RESUME_SEE;
        R r = new R();
        r.setCode(200);
        r.setMsg("显示未查看简历成功！");
        List<Resume> list = talentSaveMapper.notCheckResume(re_status);
        r.setData(list);
        return r;
    }

    /**
     * 显示未录用简历
     * @return
     */
    @Override
    public R notEmployResume() {
        Integer re_status = State.RESUME_NO_EMPLOYED;
        R r = new R();
        List<Resume> list = talentSaveMapper.notEmployResume(re_status);
        r.setCode(200);
        r.setMsg("显示未录用简历成功!");
        r.setData(list);
        return r;
    }

    /**
     * 显示已经录用简历
     * @param
     * @return
     */
    @Override
    public R alreadyEmployResume() {
        Integer re_status = State.RESUME_EMPLOYED;
        R r = new R();
        List<Resume> list = talentSaveMapper.alreadyEmployResume(re_status);
        r.setCode(200);
        r.setMsg("显示已经录用简历成功！");
        r.setData(list);
        return r;
    }

    /**
     * 显示已经工作的简历
     * @return
     */
    @Override
    public R alreadyWorkResume() {
        Integer re_status = State.RESUME_INDUCTION;
        R r = new R();
        List<Resume> list = talentSaveMapper.alreadyWorkResume(re_status);
        r.setCode(200);
        r.setMsg("显示已经工作的简历成功！");
        r.setData(list);
        return r;
    }

    /**
     * 显示未查看简历具体信息
     * @param
     * @return
     */
    @Override
    public R showNotCheckResume(ReturnResume_id returnResume_id) {
        R r = new R();
        Integer resume_id = returnResume_id.getResume_id();
        XzhResume xzhResume = talentSaveMapper.showNotCheckResume(resume_id);
        Date date = xzhResume.getRe_refreshDate();
        xzhResume.setRe_refreshDate_string(DateUtils.date2String(date));
        Date date1 = xzhResume.getRe_birthday();
        xzhResume.setRe_birthday_String(DateUtils.date2String(date1));
        Integer total_emp_number = talentSaveMapper.selectTotal_emp_number();
        xzhResume.setTotal_emp_number(total_emp_number);
        Date date2 = talentSaveMapper.selectApploy_Job_Time(resume_id);
        xzhResume.setEmp_joinTime_string(DateUtils.date2String(date2));
        if(resume_id==null){
            r.setCode(500);
            r.setMsg("显示未查看简历的具体信息失败！空值警告！");
        }else{
            r.setCode(200);
            r.setMsg("显示未查看简历的具体信息成功！");
            r.setData(xzhResume);
        }
        return r;
    }

    /**
     * 显示未录用简历的具体信息
     * @param returnResume_id
     * @return
     */
    @Override
    public R showNotEmployResume(ReturnResume_id returnResume_id) {
        R r = new R();
        Integer resume_id = returnResume_id.getResume_id();
        Resume resume = talentSaveMapper.showNotEmployResume(resume_id);
        if(resume==null){
            r.setCode(500);
            r.setMsg("显示未录用简历的具体信息失败！空值警告！");
        }else{
            r.setCode(200);
            r.setMsg("显示未录用简历的具体信息成功！");
            r.setData(resume);
        }
        return r;
    }

}
